package com.neusoft.hifly.information.view.service.impl;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.neusoft.hifly.commons.lang.LongUtil;
import com.neusoft.hifly.commons.lang.StringConstants;
import com.neusoft.hifly.core.basic.service.impl.CTSaaSBaseServiceImpl;
import com.neusoft.hifly.core.log.Log;
import com.neusoft.hifly.core.log.annotation.LogType;
import com.neusoft.hifly.core.log.annotation.OperationLog;
import com.neusoft.hifly.information.view.dao.IArticleViewDao;
import com.neusoft.hifly.information.view.entity.ArticleViewObj;
import com.neusoft.hifly.information.view.service.IArticleViewService;

/**
 * Title: 文章资讯服务
 * <p>
 * Description: 文章访问量 Service
 * <p>
 * Copyright: 2019
 * <p>
 * Company: 东软汉枫
 *
 * @author lh_neu
 * @version 1.0.0
 */
@Service
public class ArticleViewServiceImpl extends CTSaaSBaseServiceImpl implements IArticleViewService {
	@Autowired(required = false)
	private IArticleViewDao articleViewDao;

	/**
	 * 保存对象
	 *
	 * @param organCode
	 *            租户编码
	 * @param organLogicId
	 *            租户逻辑ID
	 * @param obj
	 *            对象
	 */
	@Override
	@OperationLog(method = LogType.UPDATE, msg = "文章访问量")
	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = { Exception.class, RuntimeException.class })
	public void save(final String organCode, final long organLogicId, final ArticleViewObj obj) {
		if (StringUtils.isEmpty(organCode) || obj == null) {
			return;
		}
		if (StringUtils.isEmpty(obj.getOrganCode())) {
			obj.setOrganCode(organCode);
		}
		if (LongUtil.isEmpty(obj.getOrganLogicId())) {
			obj.setOrganLogicId(organLogicId);
		}

		if (StringUtils.isNotEmpty(obj.getViewMac()) && obj.getViewMac().length() > 50) {
			// 类型ping 10.21.31.10 ……的
			Log.error("疑似刷票哦！" + obj.getViewMac());
			return;
		}
		// 保存
		obj.setId(idWorker.nextId());
		if (LongUtil.isEmpty(obj.getLogicId())) {
			obj.setLogicId(idWorker.nextId());
		}

		if (obj.getViewTime() == null) {
			obj.setViewTime(LocalDateTime.now());
		}

		obj.setOrganLogicId(organLogicId);
		this.setForm(obj, obj);

		obj.setViewTime(LocalDateTime.now());
		articleViewDao.insert(obj);

	}

	/**
	 * 根据ID删除对象
	 *
	 * @param organCode
	 *            租户编码
	 * @param organLogicId
	 *            租户逻辑ID
	 * @param logicId
	 *            对象ID
	 */
	@Override
	@OperationLog(method = LogType.DELETE, msg = "文章访问量")
	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = { Exception.class, RuntimeException.class })
	public void deleteByLogicId(final String organCode, final long organLogicId, final long logicId) {
		if (StringUtils.isEmpty(organCode)) {
			return;
		}

		articleViewDao.deleteByLogicId(organCode, organLogicId, logicId);
	}

	/**
	 * 批量删除对象
	 *
	 * @param organCode
	 *            租户编码
	 * @param organLogicId
	 *            租户逻辑ID
	 * @param logicIds
	 *            对象ID，多id使用英文逗号分隔
	 */
	@Override
	public void deleteByBatch(final String organCode, final long organLogicId, final String logicIds) {
		if (StringUtils.isEmpty(organCode) || StringUtils.isEmpty(logicIds)) {
			return;
		}
		final String[] logicIdArr = logicIds.split(StringConstants.STRING_SPLIT_FLAG_3);
		if (logicIdArr == null) {
			return;
		}
		for (final String logicId : logicIdArr) {
			if (StringUtils.isEmpty(logicId)) {
				continue;
			}
			this.deleteByLogicId(organCode, organLogicId, Long.parseLong(logicId));
		}
	}

	/**
	 * 根据id查询对象
	 *
	 * @param organCode
	 *            租户编码
	 * @param organLogicId
	 *            租户逻辑ID
	 * @param logicId
	 *            对象logicId
	 * @return 对象
	 */
	@Override
	public ArticleViewObj getByLogicId(final String organCode, final long organLogicId, final long logicId) {
		if (StringUtils.isEmpty(organCode)) {
			return null;
		}
		return articleViewDao.getByLogicId(organCode, organLogicId, logicId);
	}

	/**
	 * 查询所有对象
	 *
	 * @param organCode
	 *            租户编码
	 * @param organLogicId
	 *            租户逻辑ID
	 * @return 对象集合
	 */
	@Override
	public List<ArticleViewObj> findAll(final String organCode, final long organLogicId) {
		if (StringUtils.isEmpty(organCode)) {
			return null;
		}
		return articleViewDao.findAll(organCode, organLogicId);
	}

	/**
	 * 根据条件查询
	 *
	 * @param organCode
	 *            租户编码
	 * @param organLogicId
	 *            租户逻辑ID
	 * @param property
	 *            参数。其中isLike:Boolean 是否模糊查询
	 * @return 集合
	 */
	@Override
	public List<ArticleViewObj> search(final String organCode, final long organLogicId, Map<String, Object> property) {
		if (StringUtils.isEmpty(organCode)) {
			return null;
		}
		if (property == null) {
			property = new HashMap<String, Object>(1);
			property.put("organLogicId", organLogicId);
		}
		final QueryWrapper<ArticleViewObj> queryWrapper = new QueryWrapper<ArticleViewObj>();
		replaceProperty(property);
		createSql(property, queryWrapper, new String[] { "VIEW_TIME" });
		return articleViewDao.selectList(queryWrapper);
	}

	/**
	 * 根据条件查询
	 *
	 * @param organCode
	 *            租户编码
	 * @param organLogicId
	 *            租户逻辑ID
	 * @param offset
	 *            当前页数
	 * @param limit
	 *            分页长度
	 * @param property
	 *            参数。其中isLike:Boolean 是否模糊查询
	 * @return 返回Map
	 */
	@Override
	public IPage<ArticleViewObj> search(final String organCode, final long organLogicId, final int offset,
			final int limit, Map<String, Object> property) {
		if (StringUtils.isEmpty(organCode)) {
			return null;
		}
		if (property == null) {
			property = new HashMap<String, Object>(1);
			property.put("organLogicId", organLogicId);
		}
		final IPage<ArticleViewObj> page = new Page<ArticleViewObj>(offset, limit);
		final QueryWrapper<ArticleViewObj> queryWrapper = new QueryWrapper<ArticleViewObj>();
		replaceProperty(property);
		createSql(property, queryWrapper, new String[] { "VIEW_TIME" });
		return articleViewDao.selectPage(page, queryWrapper);
	}

	/**
	 * 复制信息
	 *
	 * @param fromObj
	 *            原信息
	 * @param toObj
	 *            新信息
	 */
	private void setForm(final ArticleViewObj fromObj, final ArticleViewObj toObj) {
		toObj.setOrganLogicId(fromObj.getOrganLogicId());
		toObj.setArticleLogicId(fromObj.getArticleLogicId());
		toObj.setViewFrom(StringUtils.trimToEmpty(fromObj.getViewFrom()));
		toObj.setViewTime(fromObj.getViewTime());
		toObj.setViewVisitor(StringUtils.trimToEmpty(fromObj.getViewVisitor()));
		toObj.setViewIp(StringUtils.trimToEmpty(fromObj.getViewIp()));
		toObj.setViewMac(StringUtils.trimToEmpty(fromObj.getViewMac()));
		toObj.setExt1(StringUtils.trimToEmpty(fromObj.getExt1()));
		toObj.setExt2(StringUtils.trimToEmpty(fromObj.getExt2()));
		toObj.setExt3(StringUtils.trimToEmpty(fromObj.getExt3()));
		toObj.setExt4(StringUtils.trimToEmpty(fromObj.getExt4()));
		toObj.setExt5(StringUtils.trimToEmpty(fromObj.getExt5()));

	}

	/**
	 * 将对象属性替换成数据库列
	 *
	 * @param property
	 *            参数
	 */
	private void replaceProperty(final Map<String, Object> property) {
		if (property != null) {
			if (property.containsKey("organLogicId")) {
				property.put("ORGAN_LOGIC_ID", property.get("organLogicId"));
				property.remove("organLogicId");
			}
			if (property.containsKey("articleLogicId")) {
				property.put("ARTICLE_LOGIC_ID", property.get("articleLogicId"));
				property.remove("articleLogicId");
			}
			if (property.containsKey("viewFrom")) {
				property.put("VIEW_FROM", property.get("viewFrom"));
				property.remove("viewFrom");
			}
			if (property.containsKey("viewTime")) {
				property.put("VIEW_TIME", property.get("viewTime"));
				property.remove("viewTime");
			}
			if (property.containsKey("viewVisitor")) {
				property.put("VIEW_VISITOR", property.get("viewVisitor"));
				property.remove("viewVisitor");
			}
			if (property.containsKey("viewIp")) {
				property.put("VIEW_IP", property.get("viewIp"));
				property.remove("viewIp");
			}
			if (property.containsKey("viewMac")) {
				property.put("VIEW_MAC", property.get("viewMac"));
				property.remove("viewMac");
			}
			if (property.containsKey("ext1")) {
				property.put("EXT1", property.get("ext1"));
				property.remove("ext1");
			}
			if (property.containsKey("ext2")) {
				property.put("EXT2", property.get("ext2"));
				property.remove("ext2");
			}
			if (property.containsKey("ext3")) {
				property.put("EXT3", property.get("ext3"));
				property.remove("ext3");
			}
			if (property.containsKey("ext4")) {
				property.put("EXT4", property.get("ext4"));
				property.remove("ext4");
			}
			if (property.containsKey("ext5")) {
				property.put("EXT5", property.get("ext5"));
				property.remove("ext5");
			}

		}
	}
}